package com.hpeu_01.day0118;

import java.util.TreeSet;

public class TreeSetDemo01 {
    public static void main(String[] args) {
        //创建TreeSet对象
        TreeSet treeSet = new TreeSet();
        Student student = new Student("张三",18);
        Student student1 = new Student("李四",18);
        Student student2 = new Student("李四",19);
        treeSet.add(student);
        treeSet.add(student1);
        treeSet.add(student2);
        System.out.println(treeSet.toString());
    }
}
class Student implements Comparable{
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Student() {
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Object o) {
        if (o instanceof Student){
            Student student = (Student) o;
            int value = this.age-student.age;
            if (value==0){
                return this.name.compareTo(student.name);
            }else if (value<0){
                return -1;
            }else
                return 1;
        }
        return 0;
    }
}